app.register.controller("UsstkTimeshareCtrl", ["$rootScope", "$scope", "$location", "$timeout", "$window", "ToastMessage", "SecurityService", "StockService", "QuotationService", "$q", function($rootScope, $scope, $location, $timeout, $window, ToastMessage, SecurityService, StockService, QuotationService, $q) {
    function loadQuoteData() {
        return $q.all([funs.fetchQuote()])
    }
    function loadTimeChartData() {
        return $q.all([funs.fetchTimeChart()])
    }
    function loadKlineData() {
        return $q.all([funs.fetchKlineData()])
    }
    function autoReload() {
        null  != autoTimer && (autoTimer = clearTimeout(autoTimer),
        autoTimer = null ),
        isExist || (autoTimer = setTimeout(function() {
            loadQuoteData()["finally"](autoReload)
        }, 4e3))
    }
    function autoReload_A() {
        null  != autoTimerA && (autoTimerA = clearTimeout(autoTimerA),
        autoTimerA = null ),
        isExist || (autoTimerA = setTimeout(function() {
            loadTimeChartData()["finally"](autoReload_A)
        }, 15e3))
    }
    function autoReload_B() {
        null  != autoTimerB && (autoTimerB = clearTimeout(autoTimerB),
        autoTimerB = null ),
        isExist || (autoTimerB = setTimeout(function() {
            loadKlineData()["finally"](autoReload_B)
        }, 12e4))
    }
    var isExist = !1
      , market = app.util.getSearch($location, "market")
      , code = app.util.getSearch($location, "code");
    $scope.chart = null ,
    $scope.charttype = "fs",
    $scope.quotation = null ,
    $scope.toBuy = function(p) {
        p ? $location.url("/usstk/buying?code=" + p.code + "&market=" + p.market) : $location.url("/usstk/buying")
    }
    ,
    $scope.toSell = function(p) {
        p ? $location.url("/usstk/selling?code=" + p.code + "&market=" + p.market) : $location.url("/usstk/selling")
    }
    ,
    $scope.getDerict = function(preClose, price) {
        return price && preClose ? price >= preClose ? "+" : "-" : "+"
    }
    ,
    $scope.typeChange = function(type) {
        $scope.charttype = type,
        "fs" == type ? (document.getElementById("canvasDk").style.display = "none",
        document.getElementById("canvasDk_div").style.display = "none",
        document.getElementById("canvasTc").style.display = "inline",
        document.getElementById("canvasTc_div").style.display = "inline",
        funs.fetchTimeChart()) : "rx" == type && (document.getElementById("canvasTc").style.display = "none",
        document.getElementById("canvasTc_div").style.display = "none",
        document.getElementById("canvasDk").style.display = "inline",
        document.getElementById("canvasDk_div").style.display = "inline",
        funs.fetchKlineData())
    }
    ,
    $scope.isOptional = function(p) {
        return p && $rootScope.optionsMap && $rootScope.optionsMap[p.market + "_" + p.code] ? !0 : !1
    }
    ,
    $scope.optChange = function(p, $event) {
        p ? $scope.delOpt($scope.quotation, $event) : $scope.addOpt($scope.quotation, $event)
    }
    ,
    $scope.addOpt = function(quotation, $event) {
        if (quotation) {
            if ($scope.optionsMap && $scope.optionsMap[quotation.market + "_" + quotation.code])
                return void $event.stopPropagation();
            var params = {
                stockMarket: quotation.market,
                stockName: quotation.name,
                stockCode: quotation.code
            };
            StockService.addOption({
                selectStock: params
            }).success(function(data) {
                ToastMessage.show(data.message, data.success),
                data && data.success && funs.loadOptions()
            }),
            $event.stopPropagation()
        }
    }
    ,
    $scope.delOpt = function(quotation, $event) {
        if (quotation) {
            if ($scope.optionsMap && !$scope.optionsMap[quotation.market + "_" + quotation.code])
                return void $event.stopPropagation();
            StockService.delOption(quotation.market, quotation.code).success(function(data) {
                ToastMessage.show(data.message, data.success),
                data && data.success && funs.loadOptions()
            }),
            $event.stopPropagation()
        }
    }
    ,
    $scope.refresh = function() {
        funs.pageLoad()
    }
    ,
    $scope.removeCrossLine = function() {
        void 0 != $scope.chart.canvas && void 0 != $scope.chart.canvas.crossLineAndTipMgrInstance && void 0 != $scope.chart.canvas.crossLineAndTipMgrInstance.clsMgr && $scope.chart.canvas.crossLineAndTipMgrInstance.clsMgr.removeCrossLines()
    }
    ;
    var funs = {
        pageLoad: function() {
            var fsWidth = .96 * document.body.clientWidth * 2
              , yscareWith = fsWidth - 45
              , minsChartOption = {
                fallColor: "green",
                riseColor: "red",
                normalColor: "#8f8f8f",
                maxDotsCount: 391,
                needPaintAvgPriceLine: !0,
                backgroundColor: "white",
                topText: {
                    font: "24px Helvetica",
                    color: "#8f8f8f",
                    fontStyle: "normal",
                    region: {
                        x: 15,
                        y: 10,
                        width: fsWidth - 10,
                        height: 36
                    },
                    textBaseline: "top"
                },
                minsChart: {
                    region: {
                        x: 6,
                        y: 50,
                        width: fsWidth,
                        height: 400
                    },
                    priceLineColor: "#157EEA",
                    avgPriceLineColor: "#f8a900",
                    middleLineColor: "#ededed",
                    otherSplitLineColor: "#ededed",
                    borderColor: "#ededed",
                    horizontalLineCount: 3,
                    verticalLineCount: 3,
                    yScalerLeft: {
                        font: "24px Helvetica",
                        region: {
                            x: 10,
                            y: 54,
                            width: 50.5,
                            height: 385
                        },
                        align: "left",
                        fontHeight: 18,
                        textBaseline: "top"
                    },
                    yScalerRight: {
                        font: "24px Helvetica",
                        region: {
                            x: yscareWith,
                            y: 54,
                            width: 40.5,
                            height: 385
                        },
                        align: "right",
                        fontHeight: 18,
                        textBaseline: "top"
                    }
                },
                xScaler: {
                    font: "24px Helvetica",
                    color: "#8f8f8f",
                    region: {
                        x: 6,
                        y: 470,
                        width: fsWidth,
                        height: 40
                    },
                    data: ["10:30", "12:00", "13:30", "15:00", "17:00"]
                },
                volume: {
                    region: {
                        x: 6,
                        y: 480,
                        width: fsWidth,
                        height: 0
                    },
                    bar: {
                        color: "green",
                        width: 2
                    },
                    borderColor: "#ededed",
                    splitLineColor: "#ededed",
                    yScaler: {
                        font: "24px Helvetica",
                        region: {
                            x: 10,
                            y: 480,
                            width: 60,
                            height: 0
                        },
                        color: "#8f8f8f",
                        align: "left",
                        fontHeight: 24,
                        textBaseline: "top"
                    }
                }
            };
            $("body").hasClass("dark-skin") && (minsChartOption.minsChart.middleLineColor = "#253245",
            minsChartOption.minsChart.otherSplitLineColor = "#253245",
            minsChartOption.minsChart.borderColor = "#253245"),
            $scope.chart = new minsChart("canvasTc",minsChartOption),
            funs.fetchQuote(),
            funs.fetchTimeChart()
        },
        fetchQuote: function() {
            var hqPromise = StockService.hq(market, code);
            return hqPromise.success(function(data) {
                data && data.success && data.result && ($scope.quotation = data.result.map)
            }),
            hqPromise
        },
        fetchTimeChart: function() {
            $scope.removeCrossLine();
            var timechartPromise = StockService.timechart(market, code);
            return timechartPromise.success(function(data) {
                data && data.success && data.result && $scope.chart.paint(data.result, "USA")
            }),
            timechartPromise
        },
        fetchKlineData: function() {
            $scope.removeCrossLine();
            var klinePromise = StockService.dailykline(market, code);
            return klinePromise.success(function(data) {
                data && data.success && data.result && data.result.quote && drawKL(void 0, getKLData(data.result.quote), !1, null , "USA")
            }),
            klinePromise
        },
        fetchWeekKlineData: function() {
            $scope.removeCrossLine(),
            StockService.weeklykline(market, code).success(function(data) {
                data && data.success && data.result && data.result.quote && drawKL(void 0, getKLData(data.result.quote))
            })
        },
        loadOptions: function() {
            StockService.listOptionals().success(function(data) {
                if (data && data.success) {
                    if ($rootScope.optionsMap = {},
                    data.result)
                        for (var i = 0; i < data.result.length; i++) {
                            var t = data.result[i];
                            $rootScope.optionsMap[t.market + "_" + t.stockCode] = !0
                        }
                    StockService.cache("optionals", $rootScope.optionsMap)
                }
            })
        }
    };
    funs.pageLoad();
    var autoTimer = null 
      , autoTimerA = null 
      , autoTimerB = null ;
    autoReload(),
    autoReload_A(),
    autoReload_B();
    var optionals = StockService.cache("optionals");
    optionals ? $rootScope.optionsMap = optionals : "/index" != $location.path() && funs.loadOptions(),
    $scope.$on("$destroy", function() {
        isExist = !0,
        $scope.removeCrossLine(),
        clearTimeout(autoTimer),
        clearTimeout(autoTimerA),
        clearTimeout(autoTimerB)
    })
}
]);
